import numpy as np
import torch
def BCM(pred,gt):#��������
    #ȡ�����ֵ
    #pred=torch.round(pred, out=None)
    #gt=torch.round(gt, out=None)
    TP=np.float(torch.sum((pred==1)&(gt==1)))
    FP=np.float(torch.sum((pred==1)&(gt==0)))
    FN=np.float(torch.sum((pred==0)&(gt==1)))
    TN=np.float(torch.sum((pred==0)&(gt==0)))
    #print(TP+FP+FN+TN)
    #print(torch.max(gt))
    #print(FN)
    #print(FP)
    return TN,FP,FN,TP

def getACC(pred,gt):
    TN,FP,FN,TP=BCM(pred,gt)
    return (TP+TN)/(TP+FP+FN+TN+1e-7)

def getF1score(pred,gt):
    p=getP(pred,gt)
    r=getR(pred,gt)
    return 2*p*r/(p+r+1e-7)
def getIoU1(pred,gt):
    TN,FP,FN,TP=BCM(pred,gt)
    iou1=TP/(FP+TP+FN+1e-7)
    return iou1
def getIoU0(pred,gt):
    TN,FP,FN,TP=BCM(pred,gt)
    iou0=TN/(FP+TN+FN+1e-7)
    return iou0
def getmIoU(pred,gt):
    iou0=getIoU0(pred,gt)
    iou1=getIoU1(pred,gt)
    return (iou0+iou1)/2
def getP(pred,gt):
    TN,FP,FN,TP=BCM(pred,gt)
    return TP/(TP+FP+1e-7)
def getR(pred,gt):
    TN,FP,FN,TP=BCM(pred,gt)
    return TP/(TP+FN+1e-7)